//
//  HJSearchResultsController.m
//  Sneakers
//
//  Created by 胡光健 on 2017/10/11.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJSearchResultsController.h"
#import "HJHomeCell.h"
#import "HJHomeDetailController.h"
#import "HJHomeModel.h"
#import "HJSearchNavBar.h"
@interface HJSearchResultsController ()<searchBarDelegate>
@property(nonatomic,strong) NSMutableArray <HJHomeModel*> * dataArr;
@end

@implementation HJSearchResultsController

-(NSMutableArray <HJHomeModel*> *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}

-(void)configNaviTitleView {
    HJSearchNavBar * navBar = [[HJSearchNavBar alloc]init];
    navBar.frame = CGRectMake(0, 0, mScreenWidth, 94);
    navBar.backBtn.hidden = NO;
    navBar.searchBtn.hidden = YES;
    navBar.delegate = self;
    navBar.searchText.text = self.str;
    [self.view addSubview:navBar];
}

-(void)searchBarTextReturnWithText:(NSString *)text {
    [self loadNetworkData];
}

-(void)searchBarBackBtnClicked {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configNaviTitleView];
    [self loadNetworkData];
    
}
-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}
-(void)configTableView {
    self.tableViewStyle = UITableViewStylePlain;
    [super configTableView];
    self.tableView.frame = CGRectMake(0, 94, mScreenWidth, mScreenHeight-94);
    [self.tableView registerCellByNibName:@"HJHomeCell"];
}
-(void)loadNetworkData {
    [TZHttpTool post:SearchAPI params:@{@"keywords":self.str} success:^(NSDictionary *result) {
        NSArray * data =[HJHomeModel mj_objectArrayWithKeyValuesArray:result[@"data"]];
        [self.dataArr removeAllObjects];
        [self.dataArr addObjectsFromArray:data];
        [self.tableView reloadData];
    } failure:^(NSString *msg) {
        [self showErrorHUDWithError:msg];
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.dataArr.count) {
        [self.tableView hideNoData];
        return self.dataArr.count;
    }else {
         [self.tableView showNoData];
        return 0;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJHomeCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"HJHomeCell"];
    Cell.selectionStyle = UITableViewCellSelectionStyleNone;
    Cell.model = self.dataArr[indexPath.row];
    
    return Cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    return 130;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HJHomeDetailController *homeDetailVC = [[HJHomeDetailController alloc]init];
    HJHomeModel *model = self.dataArr[indexPath.row];
    homeDetailVC.shoes_id = model.shoes_id;
    [self.navigationController pushViewController:homeDetailVC animated:YES];
}
@end
